package com.wolfeyes.common.typehandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.util.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * @see Talk is cheap, Show me the code. -- Linus Torvalds
 * @title 处理加解密的TypeHandler处理器
 * @author yanyljava
 * @create 2021-05-18 09:20
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListTypeHandler extends BaseTypeHandler<List<String>> {
	
	/**
	 * @title 设置参数
	 */
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
		if (null == parameter || parameter.isEmpty()) {
			ps.setString(i, null);
			return;
		}
		ps.setString(i, String.join(",", parameter));
	}
	
	/**
	 * @title 获取值
	 */
	@Override
	public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
		final String value = rs.getString(columnName);
		if (StringUtils.hasText(value)) {
			return Arrays.asList(value.split(","));
		}
		return null;
	}
	
	/**
	 * @title 获取值
	 */
	@Override
	public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		final String value = rs.getString(columnIndex);
		if (StringUtils.hasText(value)) {
			return Arrays.asList(value.split(","));
		}
		return null;
	}
	
	/**
	 * @title 获取值
	 */
	@Override
	public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		final String value = cs.getString(columnIndex);
		if (StringUtils.hasText(value)) {
			return Arrays.asList(value.split(","));
		}
		return null;
	}
	
}


